// pages/inshop/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merid:0,
    tabflag:'goods',
    info:{},
    store:{},
    goods:{total:1,current_page:1,data:[]},

  },
  getDetail(event){
    let merid = event.currentTarget.dataset.merid
    let id = event.currentTarget.dataset.id
    wx.redirectTo({
      url: '../good/index?id=' + id + '&merid=' + merid
    })
  },
  requestGoods(page) {
    if(page != 1 && page > this.data.goods.total){
      return;
    }
    app.get('merapi/index/mer_goods',{id:this.data.merid,page:page})
    .then(res => {
      console.log(res)
      if(res.data.code == 0) {
        let goods = res.data.data.goods
        for(let _ of goods.data) {
          let __ = {}
          __.id = _.id
          __.merid = _.mer_id
          __.name = _.name
          __.keywords = _.keyword.split(",")
          __.price = _.price
          let im = _.images.split(",")
          __.image = im.length > 0 && im[0]!=""?(app.globalData.remote_static + im[0]):''
          this.data.goods.data.push(__)
        }
        this.data.goods.total = goods.total
        this.data.goods.current_page = goods.current_page
        this.setData({
          goods:this.data.goods
        })
      }else {
        wx.showToast({
          title: res.data.msg,
        })
      }
    }).catch(err => {

    })
  },
  lower:function(event) {
    if (this.data.tabflag == "goods"){
      if(this.data.goods.current_page >= this.data.goods.total) {
        return;
      }
      this.requestGoods(this.data.goods.current_page + 1)
    }else if(this.data.tabflag == "show") {

    }else {

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.merid = options.id
    app.get('merapi/index/merchant', { id: this.data.merid})
    .then(res=>{
      if(res.data.code == 0) {
        res.data.data.info.mer_icon = app.globalData.remote_static + res.data.data.info.mer_icon
        res.data.data.info.contact_user = res.data.data.info.contact_user.substr(0,1) + "**"
        res.data.data.store.photo1 = app.globalData.remote_static + res.data.data.store.photo1
        res.data.data.store.photo2 = app.globalData.remote_static + res.data.data.store.photo2
        res.data.data.store.photo3 = app.globalData.remote_static + res.data.data.store.photo3
        console.log(res.data)
        this.setData({
          info:res.data.data.info,
          store:res.data.data.store
        })
      }else {
        wx.showToast({
          title: res.data.msg,
        })
      }

    }).catch(err => {
      wx.showToast({
        title: '系统错误',
      })
    })
    this.requestGoods(1)
  },
  switchTab:function(event) {
    let flag = event.currentTarget.dataset.flag
    this.setData({
      tabflag:flag
    })
  }
})